package top.yxqz.filter.demo;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.余
 * @date 2022/12/6
 * @package top.yxqz.filter.demo
 *
 * 静态资源拦截器
 */
@WebFilter(urlPatterns={"*.js","*.css"})
public class StaticResourceNeedCacheFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletResponse response  = (HttpServletResponse) servletResponse;
        HttpServletRequest request  = (HttpServletRequest) servletRequest;

//        设置缓存的代码
        long time  = 1000*60*60; //一个小时
        response.setDateHeader("Expires", System.currentTimeMillis() + time);
        filterChain.doFilter(request,response);

    }

    @Override
    public void destroy() {

    }
}
